home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 1 / PC Actual CD 01.iso / share / dos / graficos / plydat14.arj / HILBERT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-29  |  6.3 KB  |  231 lines

  1. /*
  2.  * hilbert.c - Create a hilbert curve of spheres and cylinders
  3.  *
  4.  * Version:  (1.0) 30 January 1992
  5.  * Author:  Alexander Enzmann
  6.  *
  7.  * SIZE_FACTOR determines the number of components output.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #ifdef MAC
  14. #include <console.h>
  15. #endif
  16. #include "def.h"
  17. #include "lib.h"
  18.  
  19. #define SIZE_FACTOR 5
  20.  
  21. static char *txname;
  22. static double last_x, last_y, x, y;
  23. static double offset_x, offset_y;
  24.  
  25. static void a(int i, double h);
  26. static void b(int i, double h);
  27. static void c(int i, double h);
  28. static void d(int i, double h);
  29.  
  30.  
  31. static void
  32. plot(double x, double y, double h)
  33. {
  34.    COORD4 c0, c1;
  35.  
  36.    /* cylinder from (last_x, last_y) -> (x, y) */
  37.    SET_COORD4(c0, last_x+offset_x, 0.0, last_y+offset_y, h/4.0);
  38.    SET_COORD4(c1, x+offset_x, 0.0, y+offset_y, h/4.0);
  39.    lib_output_cylcone(&c0, &c1, txname);
  40.  
  41.    /* sphere at (x, y) */
  42.    lib_output_sphere(&c1, txname);
  43.  
  44.    /* Reset last position */
  45.    last_x = x;
  46.    last_y = y;
  47. }
  48.  
  49. static void
  50. a(int i, double h)
  51. {
  52.    if (i>0) {
  53.       d(i-1, h); x -= h; plot(x, y, h);
  54.       a(i-1, h); y -= h; plot(x, y, h);
  55.       a(i-1, h); x += h; plot(x, y, h);
  56.       b(i-1, h);
  57.       }
  58. }
  59.  
  60. static void
  61. b(int i, double h)
  62. {
  63.    if (i>0) {
  64.       c(i-1, h); y += h; plot(x, y, h);
  65.       b(i-1, h); x += h; plot(x, y, h);
  66.       b(i-1, h); y -= h; plot(x, y, h);
  67.       a(i-1, h);
  68.       }
  69. }
  70.  
  71. static void
  72. c(int i, double h)
  73. {
  74.    if (i>0) {
  75.       b(i-1, h); x += h; plot(x, y, h);
  76.       c(i-1, h); y += h; plot(x, y, h);
  77.       c(i-1, h); x -= h; plot(x, y, h);
  78.       d(i-1, h);
  79.       }
  80. }
  81.  
  82. static void
  83. d(int i, double h)
  84. {
  85.    if (i>0) {
  86.       a(i-1, h); y -= h; plot(x, y, h);
  87.       d(i-1, h); x -= h; plot(x, y, h);
  88.       d(i-1, h); y += h; plot(x, y, h);
  89.       c(i-1, h);
  90.       }
  91. }
  92.  
  93. static void
  94. create_hilbert(int depth, double width)
  95. {
  96.    COORD4 center;
  97.    int i = 0;
  98.    double h = width;
  99.  
  100.    for (i=0;i<depth;i++)
  101.       h /= 2.0;
  102.  
  103.    /* Output a sphere at the start of the curve */
  104.    offset_x = offset_y = width / 2.0;
  105.    SET_COORD4(center, offset_x, 0.0, offset_y, h/4.0) ;
  106.    lib_output_sphere(¢er, txname);
  107.    last_x = last_y = 0.0;
  108.  
  109.    /* Crank out the rest of the curve */
  110.    a(depth, h);
  111. }
  112.  
  113. static void
  114. create_textured_plane()
  115. {
  116.    printf("define test_map\n");
  117.    printf("   color_map(\n");
  118.    printf("      [0,   0.1, red,     orange]\n");
  119.    printf("      [0.1, 0.3, orange,  blue]\n");
  120.    printf("      [0.3, 0.5, blue,    skyblue]\n");
  121.    printf("      [0.5, 0.7, skyblue, orange]\n");
  122.    printf("      [0.7, 0.9, orange,  magenta]\n");
  123.    printf("      [0.9, 1.0, magenta, red],\n");
  124.    printf("      <1, 1, 1>)\n\n");
  125.    printf("# Simple color map texture\n");
  126.    printf("define noise_texture\n");
  127.    printf("texture {\n");
  128.    printf("   special surface {\n");
  129.    printf("      color test_map[noise(P)]\n");
  130.    printf("      ambient 0.2\n");
  131.    printf("      diffuse 0.8\n");
  132.    printf("      specular white, 0.5\n");
  133.    printf("      microfacet Reitz 10\n");
  134.    printf("      reflection white, 0.2\n");
  135.    printf("      }\n");
  136.    printf("   scale <0.3, 0.3, 0.3>\n");
  137.    printf("   }\n\n");
  138.    printf("define marble_turb 4\n");
  139.    printf("define marble_fn  (sawtooth(P[0] +");
  140.    printf(" marble_turb * noise(P,8)) + 1) / 2\n\n");
  141.    printf("define white_marble_map\n");
  142.    printf("   color_map(\n");
  143.    printf("      [0.0, 0.8, <1.0, 1.0, 1.0>, <0.7, 0.7, 0.7>]\n");
  144.    printf("      [0.8, 1.0, <0.7, 0.7, 0.7>, <0.1, 0.1, 0.1>],\n");
  145.    printf("      white)\n\n");
  146.    printf("# Simple marble textures\n");
  147.    printf("define white_marble\n");
  148.    printf("texture {\n");
  149.    printf("   special surface {\n");
  150.    printf("      color white_marble_map[marble_fn]\n");
  151.    printf("      ambient 0.2\n");
  152.    printf("      diffuse 0.8\n");
  153.    printf("      specular white, 0.5\n");
  154.    printf("      microfacet Reitz 10\n");
  155.    printf("      reflection white, 0.2\n");
  156.    printf("      }\n");
  157.    printf("   scale <0.6, 0.6, 0.6>\n");
  158.    printf("   }\n\n");
  159.    printf("object {\n");
  160.    printf("   polygon 4, <-20, -0.4, -20>, <-20, -0.4, 20>,\n");
  161.    printf("              < 20, -0.4,  20>, < 20, -0.4,-20>\n");
  162.    printf("   texture {\n");
  163.    printf("      hexagon white_marble, txt000, noise_texture\n");
  164.    printf("      scale <0.2, 0.2, 0.2>\n");
  165.    printf("      }\n");
  166.    printf("   }\n\n");
  167. }
  168.  
  169. void
  170. main(int argc, char *argv[])
  171. {
  172.     COORD4  back_color, curve_color ;
  173.     COORD4  center_pt, light, dir;
  174.     COORD4  from, at, up, msphere;
  175.     double offset = 1.0;
  176.     int i;
  177.  
  178. #ifdef MAC
  179.    argc = ccommand(&argv);
  180. #endif
  181.  
  182.     /* output viewpoint */
  183.     SET_COORD(from, 0.0, 5.0,-3.0) ;
  184.     SET_COORD(at,   0.0, 0.0, 0.0) ;
  185.     SET_COORD(up,   0.0, 1.0, 0.0) ;
  186.     lib_output_viewpoint( &from, &at, &up, 30.0, 1.0, 1.0, 256, 256);
  187.  
  188.     /* output background color - dark blue */
  189.     SET_COORD( back_color, 0.039, 0.18, 0.376 ) ;
  190.     lib_output_background_color( &back_color ) ;
  191.  
  192.     /* output light source */
  193.     SET_COORD( light,-5.0, 10.0,-20.0) ;
  194.     lib_output_light( &light ) ;
  195.     SET_COORD( light, 5.0, 10.0, 20.0) ;
  196.     lib_output_light( &light ) ;
  197.  
  198.     /* Output bounding slabs oriented along the coordinate axes */
  199.     SET_COORD(dir, 1.0, 0.0, 0.0);
  200.     lib_output_bounding_slab(&dir);
  201.     SET_COORD(dir, 0.0, 1.0, 0.0);
  202.     lib_output_bounding_slab(&dir);
  203.     SET_COORD(dir, 0.0, 0.0, 1.0);
  204.     lib_output_bounding_slab(&dir);
  205.  
  206.     /* Make some mirrored spheres below the hilbert curve */
  207.     SET_COORD(curve_color, 1.0, 1.0, 1.0 ) ;
  208.     txname = lib_output_color(&curve_color, 0.1, 0.2, 0.2, 5.0, 1.0, 0.0, 0.0);
  209.     
  210.     for (i=1;i<SIZE_FACTOR;i++)
  211.        offset /= 2.0;
  212.     SET_COORD4(msphere,offset-1.0,-0.2, offset-1.0, 0.2);
  213.     lib_output_sphere(&msphere, txname);
  214.     SET_COORD4(msphere,offset-1.0,-0.2, 1, 0.2);
  215.     lib_output_sphere(&msphere, txname);
  216.     SET_COORD4(msphere,1,-0.2, offset-1.0, 0.2);
  217.     lib_output_sphere(&msphere, txname);
  218.     SET_COORD4(msphere,1,-0.2, 1, 0.2);
  219.     lib_output_sphere(&msphere, txname);
  220.  
  221.     /* Dump out a plane with hexagonal tiling */
  222.     create_textured_plane();
  223.  
  224.     /* output color - red */
  225.     SET_COORD(curve_color, 1.0, 0.2, 0.2 ) ;
  226.     txname = lib_output_color(&curve_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0) ;
  227.  
  228.     /* compute and output hilbert curve */
  229.     create_hilbert(SIZE_FACTOR, 2.0);
  230. }
  231.